package com.banciyuan.bcywebview.biz.main.newdiscover.ranklist;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.DiscoverApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.RankItem;


public class RankListActivity extends BaseActivity {

    private RequestQueue mQueue;
    private RecyclerView mRecyclerView;
    private ProgressbarHelper mProgressbarHelper;
    private List<RankItem.RankInfo> interestRankList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ranklist_layout);
        initArgs();
        initUi();
        initActionbar();
        initProgressbar();
        initData();
    }

    @Override
    protected void initArgs() {
        mQueue = VolleyQueue.getRquest(this);
    }

    @Override
    protected void initProgressbar() {
        View mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.all_site_ranking_list));

    }

    @Override
    protected void initUi() {
        mRecyclerView = (RecyclerView) findViewById(R.id.ranklist_recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
    }


    @Override
    protected void initData() {
        String url = HttpUtils.BASE_URL + DiscoverApi.getAlTopList();
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, RankListActivity.this)) {
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        List<RankItem> rankItems = new Gson().fromJson(jsonObject.getString(HttpUtils.UPLOAD_DATA),
                                new TypeToken<List<RankItem>>() {
                                }.getType());
                        processData(rankItems);
                        mProgressbarHelper.onSuccess();
                    } catch (Exception e) {
                        e.printStackTrace();
                        onError();
                    }
                } else {
                    onError();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
                onError();
            }
        };
        OnErrorProxy errorProxy = new OnErrorProxy(error, succ, url, this, null);
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, null, succ, errorProxy);
        mQueue.add(stringRequest);
    }

    private void processData(List<RankItem> tempRanks) {
        if (null == tempRanks)
            return;
        getInterestRankData(tempRanks);
        List<RankItem> rankItems = new ArrayList<>();
        for (int i = 0; i < tempRanks.size(); i++) {
            RankItem tempRank = tempRanks.get(i);
            rankItems.add(new RankItem(tempRank.getName(), "line_type"));
            List<RankItem.RankInfo> tempInfos = tempRank.getInfo();
            if (null != tempInfos) {
                int size = tempInfos.size() % 4 == 0 ? tempInfos.size() / 4 : tempInfos.size() / 4 + 1;
                for (int j = 0; j < size; j++) {
                    RankItem rankItem = new RankItem();
                    int fromIndex = j * 4;
                    int toIndex = j * 4 + 4 > tempInfos.size() ? tempInfos.size() : j + 4;
                    List<RankItem.RankInfo> innerRankInfos = tempInfos.subList(fromIndex, toIndex);
                    for (RankItem.RankInfo innerRankInfo : innerRankInfos) {
                        innerRankInfo.setCate(tempRank.getCate());
                        innerRankInfo.setRank_type(tempRank.getRank_type());
                    }
                    rankItem.setInfo(innerRankInfos);
                    rankItem.setItemType("main_type");
                    rankItems.add(rankItem);
                }
            }
        }

        RankListAdapter rankListAdapter = new RankListAdapter(this, rankItems);
        mRecyclerView.setAdapter(rankListAdapter);
    }

    private void getInterestRankData(List<RankItem> tempRanks) {
        for (int i = 0; i < tempRanks.size(); i++) {
            if ("top_insterests".equals(tempRanks.get(i).getCate())) {
                interestRankList.addAll(tempRanks.get(i).getInfo());
            }
        }
    }

    private void onError() {
        mProgressbarHelper.onFailed();
    }

    public void gotoIntersetRankActivity(String name) {
        if (null != interestRankList && !interestRankList.isEmpty()) {
            Intent intent = new Intent(this, InterestRankActivity.class);
            intent.putExtra(gotoUtil.INTENT_VALUE_ONE, (Serializable) interestRankList);
            intent.putExtra(gotoUtil.INTENT_VALUE_TWO, name);
            startActivity(intent);
        }
    }

}
